package com.fangfang.home;

import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.theme.ThemeHelper;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.bottomnavigation.BottomNavigationBar;
import com.common.ui.bottomnavigation.BottomNavigationItem;
import com.fangfang.R;
import com.fangfang.user.UserFragment;
import com.fangfang.video.VideoFragment;
import com.fangfang.voice.VoiceFragment;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;

import butterknife.BindView;

public class HomeActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener{

    @BindView(R.id.content_container)
    LinearLayout contentContainer;
    @BindView(R.id.bottomNavigationBar)
    BottomNavigationBar bottomNavigationBar;
    private BottomNavigationItem bottomNavigationItemOne, bottomNavigationItemTwo,
            bottomNavigationItemThree, bottomNavigationItemFour;
    private HomeFragment homeFragment;
    private VideoFragment videoFragment;
    private VoiceFragment voiceFragment;
    private UserFragment userFragment;
    private int index = 0;//0首页 1视频 2音频 3用户中心
    private long exitTime = 0;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.white)
                .statusBarColor(R.color.white)
                //导航栏图标是深色，不写默认为亮色
                .navigationBarDarkIcon(true)
                //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                .autoDarkModeEnable(true)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_home;
    }

    @Override
    public void init() {
        initViews();
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    @Override
    public void onTabSelected(int position) {
        index = position;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        hideFragments(transaction);
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content_container, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                transaction.commit();
                break;
            case 1:
                if (videoFragment == null) {
                    videoFragment = new VideoFragment();
                    transaction.add(R.id.content_container, videoFragment);
                } else {
                    transaction.show(videoFragment);
                }
                transaction.commit();
                break;
            case 2:
                if (voiceFragment == null) {
                    voiceFragment = new VoiceFragment();
                    transaction.add(R.id.content_container, voiceFragment);
                } else {
                    transaction.show(voiceFragment);
                }
                transaction.commit();
                break;
            case 3:
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    transaction.add(R.id.content_container, userFragment);
                } else {
                    transaction.show(userFragment);
                }
                transaction.commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    private void initViews(){
        /* InputMethodManager inputMethodManager =
                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);*/
        /**
         * BadgeItem 红点
         */
       /* BadgeItem badgeItem=new BadgeItem().setBorderWidth(1).setBackgroundColorResource(R.color.colorAccent)
                .setText("2").setHideOnSelect(true);*/
        //bottomNavigationBar.setFab(floatingActionButton);

        //如果Item的个数<=3就会使用MODE_FIXED模式，否则使用MODE_SHIFTING模式
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_DEFAULT);
        //填充模式，未选中的Item会显示文字，没有换挡动画。
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        //换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
        //bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);

        //如果设置的Mode为MODE_FIXED，将使用BACKGROUND_STYLE_STATIC 。如果Mode为MODE_SHIFTING将使用BACKGROUND_STYLE_RIPPLE。
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        //点击的时候没有水波纹效果
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        //点击的时候有水波纹效果
        //bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);

       /* bottomNavigationItemOne = new BottomNavigationItem(R.mipmap.user, getString(R.string.home))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemTwo = new BottomNavigationItem(R.mipmap.user, getString(R.string.video))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemThree = new BottomNavigationItem(R.mipmap.user, getString(R.string.voice))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemFour = new BottomNavigationItem(R.mipmap.user, getString(R.string.user))
                .setActiveColorResource(R.color.black);*/
        bottomNavigationItemOne = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_home)),
                getString(R.string.home))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemTwo = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_videocam)),
                getString(R.string.video))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemThree = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_queue_music)),
                getString(R.string.voice))
                .setActiveColorResource(R.color.black);
        bottomNavigationItemFour = new BottomNavigationItem(ThemeHelper.getToolbarIcon(this, (GoogleMaterial.Icon.gmd_child_care)),
                getString(R.string.user))
                .setActiveColorResource(R.color.black);

        bottomNavigationBar
                .addItem(bottomNavigationItemOne)
                .addItem(bottomNavigationItemTwo)
                .addItem(bottomNavigationItemThree)
                .addItem(bottomNavigationItemFour)
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        //AppDroid.getInstance().resumePush();
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        transaction.replace(R.id.content_container, homeFragment);
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {

        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (videoFragment != null) {
            transaction.hide(videoFragment);
        }
        if (voiceFragment != null) {
            transaction.hide(voiceFragment);
        }
        if (userFragment != null) {
            transaction.hide(userFragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
